package demo2;

/**
 * 判断一个字符串是否是回文子串
 * 默认空字符串是回文子串
 */
public class PalinString {
    public static void main(String[] args) {
        String s1 = "";
        System.out.println(isPalinString(s1));
        String s2 = "   ";
        System.out.println(isPalinString(s2));
        String s3 = "abcba";
        System.out.println(isPalinString(s3));
        String s4 = "gth";
        System.out.println(isPalinString(s4));

    }

    public static boolean isPalinString(String str){
        if(str.length()==0 || str.trim().length()==0) return true;

        int i = 0;
        int j = str.length()-1;

        // 从首尾往中间走
        while(i < j){
            if(str.charAt(i)!=str.charAt(j)){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }
}

